DAY3:Jaden Casting Strings


Posted by birdbirdmurmur on 2023-07-16

題目連結:

Jaden Casting Strings

解題過程:

  • 取得每個單字的第一個字
  • slice()後的陣列很特別
    • 可以暫時儲存一個陣列(slice array)
  • 把第一個字轉成大寫
    • toUpperCase()會把整個string都轉換
  • 把大寫字和原本的單字合併
  • 反過來做一次迴圈
  • 回傳

解法:

String.prototype.toJadenCase = function () {
  const words = this.split(' ')
  const answer = words.map( (word) =>{
    const first = word.charAt(0).toUpperCase();
    const rest = word.slice(1);
    return first + rest;
  })
  return answer.join(' ')
};

延伸思考:

對字串還沒有很熟練
一直翻MDN資料來看
但越來越懂string和array之間的關係了!


#javascript #Codewars #split #map #charat #toUpperCase #slice #join







Related Posts

1731. The Number of Employees Which Report to Each Employee

1731. The Number of Employees Which Report to Each Employee

AJAX POST API 講解(筆記)

AJAX POST API 講解(筆記)

ASP.NET Core Web API 入門教學 - 使用PATCH局部更新資料

ASP.NET Core Web API 入門教學 - 使用PATCH局部更新資料


Comments